package control;

import utils.DBConnection;

import java.util.List;
import java.util.ArrayList;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;

/**
 * Created by IntelliJ IDEA.
 * User: Shawn
 * Date: 2009-6-4
 * Time: 23:57:53
 */
public class Computer {

  public static int addComputer(Connection con, ComputerInfo computer) throws SQLException {
    String sql = "INSERT INTO computer VALUES (NULL, ?, ?, ?)";
    PreparedStatement ps = con.prepareStatement(sql);
    ps.setString(1, computer.getZkzh());
    ps.setString(2, computer.getIp());
    ps.setInt(3, computer.getState());
    ps.executeUpdate();
    return DBConnection.getLastID(con);
  }

  public static void modiComputer(Connection con, ComputerInfo computer) throws SQLException {
    String sql = "UPDATE computer SET zkzh=?, ip=?, state=? WHERE cid=? LIMIT 1";
    PreparedStatement ps = con.prepareStatement(sql);
    ps.setString(1, computer.getZkzh());
    ps.setString(2, computer.getIp());
    ps.setInt(3, computer.getState());
    ps.setInt(4, computer.getCid());
    ps.executeUpdate();
  }

  public static void delComputer(Connection con) throws SQLException {
    String sql = "DELETE FROM computer";
    PreparedStatement ps = con.prepareStatement(sql);
    ps.executeUpdate();
  }

  public static ComputerInfo getComputer(Connection con, int cid) throws SQLException {
    String sql = "SELECT * FROM computer WHERE cid=? LIMIT 1";
    PreparedStatement ps = con.prepareStatement(sql);
    ps.setInt(1, cid);
    ResultSet rs = ps.executeQuery();
    return rs.next() ? rs2Info(rs) : new ComputerInfo();
  }

  public static ComputerInfo getComputer(Connection con, String ip) throws SQLException {
    String sql = "SELECT * FROM computer WHERE ip=? LIMIT 1";
    PreparedStatement ps = con.prepareStatement(sql);
    ps.setString(1, ip);
    ResultSet rs = ps.executeQuery();
    return rs.next() ? rs2Info(rs) : new ComputerInfo();
  }

  public static ComputerInfo getComputerByZkzh(Connection con, String zkzh) throws SQLException {
    String sql = "SELECT * FROM computer WHERE zkzh=? LIMIT 1";
    PreparedStatement ps = con.prepareStatement(sql);
    ps.setString(1, zkzh);
    ResultSet rs = ps.executeQuery();
    return rs.next() ? rs2Info(rs) : new ComputerInfo();
  }

  public static List<ComputerInfo> getComputerList(Connection con) throws SQLException {
    String sql = "SELECT * FROM computer";
    ResultSet rs = con.prepareStatement(sql).executeQuery();
    List<ComputerInfo> list = new ArrayList<ComputerInfo>();
    while (rs.next()) list.add(rs2Info(rs));
    return list;
  }

  private static ComputerInfo rs2Info(ResultSet rs) throws SQLException {
    ComputerInfo info = new ComputerInfo();
    info.setCid(rs.getInt("cid"));
    info.setZkzh(rs.getString("zkzh"));
    info.setIp(rs.getString("ip"));
    info.setState(rs.getInt("state"));
    return info;
  }
}
